<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.13"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>how-to/c/openChannels.c</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
  $(document).ready(initResizable);
</script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="kvaser.gif"/></td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.13 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
$(function() {
  initMenu('',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
</script>
<div id="main-nav"></div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
      <div id="nav-sync" class="sync"></div>
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
$(document).ready(function(){initNavTree('how-to_2c_2open_channels_8c-example.html','');});
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div class="header">
  <div class="headertitle">
<div class="title">how-to/c/openChannels.c</div>  </div>
</div><!--header-->
<div class="contents">
<p>This example shows how to open a channel in CAN and CAN FD mode. See <a class="el" href="page_user_guide_chips_channels.html">Open Channel</a> for more information.</p>
<p>------------------------------------------------------------------------------</p>
<div class="fragment"><div class="line"><span class="preprocessor">#include &lt;<a class="code" href="canlib_8h.html">canlib.h</a>&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;stdio.h&gt;</span></div><div class="line"></div><div class="line"><span class="comment">// Print status message if stat contains an error code</span></div><div class="line"><span class="keyword">static</span> <span class="keywordtype">void</span> check(<span class="keyword">const</span> <span class="keywordtype">char</span>* <span class="keywordtype">id</span>, <a class="code" href="canstat_8h.html#a52b5e5c71832b0bd3c6a5b1fd48583e7">canStatus</a> stat)</div><div class="line">{</div><div class="line">        <span class="keywordflow">if</span> (stat != <a name="a0"></a><a class="code" href="canstat_8h.html#a52b5e5c71832b0bd3c6a5b1fd48583e7a49743d0d438957118b9c6af2e831b209">canOK</a>) {</div><div class="line">                <span class="keywordtype">char</span> buf[50];</div><div class="line">                buf[0] = <span class="charliteral">&#39;\0&#39;</span>;</div><div class="line">                <span class="comment">// Retreive informational text about the status code</span></div><div class="line">                <a name="a1"></a><a class="code" href="group__can__general.html#ga01a7a415c95c579750bcdd95a1d245c4">canGetErrorText</a>(stat, buf, <span class="keyword">sizeof</span>(buf));</div><div class="line">                printf(<span class="stringliteral">&quot;%s: failed, stat=%d (%s)\n&quot;</span>, <span class="keywordtype">id</span>, (<span class="keywordtype">int</span>)stat, buf);</div><div class="line">        }</div><div class="line">} <span class="comment">// check</span></div><div class="line"></div><div class="line"><a class="code" href="canlib_8h.html#ae3d1b041d62207d5336f93c089cd5b65">canHandle</a> open_channel_as_can(<span class="keywordtype">int</span> channel_number)</div><div class="line">{</div><div class="line">        <a class="code" href="canlib_8h.html#ae3d1b041d62207d5336f93c089cd5b65">canHandle</a> hnd;</div><div class="line">        <a class="code" href="canstat_8h.html#a52b5e5c71832b0bd3c6a5b1fd48583e7">canStatus</a> stat;</div><div class="line"></div><div class="line">        <span class="comment">// open channel in CAN mode</span></div><div class="line">        hnd = <a name="a2"></a><a class="code" href="group___c_a_n.html#gac377d182232fb4ec2fed881c2b9ab300">canOpenChannel</a>(channel_number, 0);</div><div class="line">        <span class="keywordflow">if</span> (hnd &lt; 0) {</div><div class="line">                check(<span class="stringliteral">&quot;canOpenChannel&quot;</span>, (<a class="code" href="canstat_8h.html#a52b5e5c71832b0bd3c6a5b1fd48583e7">canStatus</a>)hnd);</div><div class="line">                <span class="keywordflow">return</span> hnd;</div><div class="line">        }</div><div class="line">        <span class="comment">// set the bus speed to 500 kbit/s</span></div><div class="line">        stat = <a name="a3"></a><a class="code" href="group___c_a_n.html#ga7eb8c2e92cfae57e7ec5031818524301">canSetBusParams</a>(hnd, <a name="a4"></a><a class="code" href="canlib_8h.html#ae72ee3c12379c4817de3b53c7cc6d89c">canBITRATE_500K</a>, 0, 0, 0, 0, 0);</div><div class="line">        check(<span class="stringliteral">&quot;canSetBusParams&quot;</span>, stat);</div><div class="line">        <span class="keywordflow">return</span> hnd;</div><div class="line">}</div><div class="line"></div><div class="line"><a class="code" href="canlib_8h.html#ae3d1b041d62207d5336f93c089cd5b65">canHandle</a> open_channel_as_canfd(<span class="keywordtype">int</span> channel_number)</div><div class="line">{</div><div class="line">        <a class="code" href="canlib_8h.html#ae3d1b041d62207d5336f93c089cd5b65">canHandle</a> hnd;</div><div class="line">        <a class="code" href="canstat_8h.html#a52b5e5c71832b0bd3c6a5b1fd48583e7">canStatus</a> stat;</div><div class="line"></div><div class="line">        <span class="comment">// open channel in CAN FD mode</span></div><div class="line">        hnd = <a class="code" href="group___c_a_n.html#gac377d182232fb4ec2fed881c2b9ab300">canOpenChannel</a>(channel_number, <a name="a5"></a><a class="code" href="canlib_8h.html#a10aad075bfb9891aa98cf3c131977dc3">canOPEN_CAN_FD</a> | <a name="a6"></a><a class="code" href="canlib_8h.html#afacd9e186d273d0dc24956ea0d433822">canOPEN_EXCLUSIVE</a>);</div><div class="line">        <span class="keywordflow">if</span> (hnd &lt; 0) {</div><div class="line">                check(<span class="stringliteral">&quot;canOpenChannel&quot;</span>, (<a class="code" href="canstat_8h.html#a52b5e5c71832b0bd3c6a5b1fd48583e7">canStatus</a>)hnd);</div><div class="line">                <span class="keywordflow">return</span> hnd;</div><div class="line">        }</div><div class="line">        <span class="comment">// set the arbitration bitrate to 500 kbit/s, with sampling point to 80%,</span></div><div class="line">        <span class="comment">// and data phase bitrate to 1000 kbit/s, with sampling point at 80%</span></div><div class="line">        stat = <a class="code" href="group___c_a_n.html#ga7eb8c2e92cfae57e7ec5031818524301">canSetBusParams</a>(hnd, <a name="a7"></a><a class="code" href="canlib_8h.html#a71dec626b7de7ddd18daf93e535902f4">canFD_BITRATE_500K_80P</a>, 0, 0, 0, 0, 0);</div><div class="line">        check(<span class="stringliteral">&quot;canSetBusParams&quot;</span>, stat);</div><div class="line">        stat = <a name="a8"></a><a class="code" href="group___c_a_n.html#gafcd85fbac103dcb123f4cd609be6fa14">canSetBusParamsFd</a>(hnd, <a name="a9"></a><a class="code" href="canlib_8h.html#a3a93c64ba76ebe3c5e525d4dfa4ade8d">canFD_BITRATE_1M_80P</a>, 0, 0, 0);</div><div class="line">        check(<span class="stringliteral">&quot;canSetBusParamsFD&quot;</span>, stat);</div><div class="line"></div><div class="line">        <span class="comment">// return handle to channel</span></div><div class="line">        <span class="keywordflow">return</span> hnd;</div><div class="line">}</div></div><!-- fragment --> </div><!-- contents -->
</div><!-- doc-content -->
<!-- HTML footer for doxygen 1.8.13-->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
  <ul>
    <li class="footer"> (canlib 5.38) Tue Feb 15 2022</li>
  </ul>
</div>
</body>
</html>
